// 面包屑数据
import { routes } from '@/router'
// console.log(routes)
function flatten(data, path = '') {
  return data.reduce((arr, item) => {
    if (!path) {
      item.path = item.path
    } else if (path === '/') {
      item.path = path + item.path
    } else {
      item.path = path + '/' + item.path
    }
    if (item.meta) {
      let parent = item.meta.parentPath
      item.meta.parentPath = path
      item.meta.parent = parent
    }
    if (item.children) {
      return arr.concat([item], flatten(item.children, item.path));
    }
    arr.push(item)
    return arr
  }, [])
}
let flatRoute = flatten(JSON.parse(JSON.stringify(routes)))
// console.log(flatRoute)
let flatRouteJSON = {}
flatRoute.forEach(item => {
  flatRouteJSON[item.path] = item
})
// console.log(flatRouteJSON)
sessionStorage.setItem('flatRouteJSON', JSON.stringify(flatRouteJSON))
const breadcrumbData = {
  state: {
    breadcrumbRoute: [],
    flatRouteJSON: flatRouteJSON
  },
  mutations: {
    SET_BREADCRUMB_ROUTE: (state, data) => {
      state.breadcrumbRoute = data
    }
  },
  actions: {
    setBreadcrumbData({ commit }, data) {
      let obj = JSON.stringify(data)
      sessionStorage.setItem('breadcrumbRoute', obj)
      commit('SET_BREADCRUMB_ROUTE', data)
    }
  }
}
export default breadcrumbData